<?php

/************************************************************
 * 商品分类处理工具
 * time		2014-08-20
 * author	liuricheng
 * 
 ***********************************************************/

/**
 * 获取分类信息
 * @param $cat_id
 */
function get_cat_info($cat_id){	
	$CommonModel	=	new CommonModel();
    $sql 	= 	"SELECT * FROM " . 'ybr_category' . " WHERE cat_id='$cat_id' LIMIT 1";
    return $CommonModel->getRow($sql);
}

/**
 * 获取分类属性列表 
 * 
 */
function get_attr_list(){
    $sql 	= 	"SELECT a.attr_id, a.cat_id, a.attr_name ".
	           	" FROM " . 'ybr_attribute'. " AS a,  ".
	           	'ybr_goods_type' . " AS c ".
	           	" WHERE  a.cat_id = c.cat_id AND c.enabled = 1 ".
	           	" ORDER BY a.cat_id , a.sort_order";
	
    //$arr = $GLOBALS['db']->getAll($sql);
    $arr	=	M()->query($sql);
    $list 	= 	array();
    
    foreach ($arr as $val){
        $list[$val['cat_id']][] 	= 	array($val['attr_id']=>$val['attr_name']);
    }
    return $list;
}

/**
 * 获得商品类型的列表
 *
 * @access  public
 * @param   integer     $selected   选定的类型编号
 * @return  string
 */
function goods_type_list($selected){	
    $sql 	= 	'SELECT cat_id, cat_name FROM ' . 'ybr_goods_type' . ' WHERE enabled = 1';
    $res 	= 	M()->query($sql); 
    
    $list 	= 	'';
    /*while ($row = $GLOBALS['db']->fetchRow($res)){
        $lst .= "<option value='$row[cat_id]'";
        $lst .= ($selected == $row['cat_id']) ? ' selected="true"' : '';
        $lst .= '>' . htmlspecialchars($row['cat_name']). '</option>';
    }*/
    foreach($res as $row){
    	$list .= "<option value='$row[cat_id]'";
        $list .= ($selected == $row['cat_id']) ? ' selected="true"' : '';
        $list .= '>' . htmlspecialchars($row['cat_name']). '</option>';
    }
    return $list;
}


/**
 * 推荐分类
 * @access  public
 * @param   array   $recommend_type 
 * @param   integer $cat_id    分类id
 *
 * @return void
 */
function insert_cat_recommend($recommend_type, $cat_id){  
    if (!empty($recommend_type)){
        //
        $recommend_res = M()->query("SELECT recommend_type FROM " . "ybr_cat_recommend" . " WHERE cat_id=" . $cat_id);
        if (empty($recommend_res)){
            foreach($recommend_type as $data){
                $data = intval($data);
                M()->execute("INSERT INTO " . "ybr_cat_recommend" . "(cat_id, recommend_type) VALUES ('$cat_id', '$data')");
            }
        }
        else{
            $old_data = array();
            foreach($recommend_res as $data){
                $old_data[] = $data['recommend_type'];
            }
            $delete_array = array_diff($old_data, $recommend_type);
            if (!empty($delete_array)){
                M()->execute("DELETE FROM " . "ybr_cat_recommend" . " WHERE cat_id=$cat_id AND recommend_type " . db_create_in($delete_array));
            }
            $insert_array = array_diff($recommend_type, $old_data);
            if (!empty($insert_array)){
                foreach($insert_array as $data){
                    $data = intval($data);
                    M()->execute("INSERT INTO " . "ybr_cat_recommend" . "(cat_id, recommend_type) VALUES ('$cat_id', '$data')");
                }
            }
        }
    }
    else{
        M()->execute("DELETE FROM ". "ybr_cat_recommend" . " WHERE cat_id=" . $cat_id);
    }
}

/**
 * 检查分类是否已经存在
 *
 * @param   string      $cat_name       分类名称
 * @param   integer     $parent_cat     上级分类
 * @param   integer     $exclude        排除的分类ID
 *
 * @return  boolean
 */
function cat_exists($cat_name, $parent_cat, $exclude = 0){
	$CommonModel	=	new CommonModel();
    $sql 			= 	"SELECT COUNT(*) FROM " . 'ybr_category' . " WHERE parent_id = '$parent_cat' AND cat_name = '$cat_name' AND cat_id<>'$exclude'";
    return ($CommonModel->getOne($sql) > 0) ? true : false;
}

/**
 * 自动返回表名字
 * @param	$tableName 
 * @example	order_info
 * @return	OrderInfo
 * 
 */
function auto_table_name($table){
	$table_name_array	=	explode("_", $table);
	$tableName			=	'';
	if(count($table_name_array) > 1){
		foreach($table_name_array as $name){
			$tableName 	.=	ucwords($name);
		}
	}else{
		$tableName	=	ucwords( $table_name_array[0]);
	}
	return $tableName;
}




